#include "stdlib.h"
#include "stdio.h"

typedef struct {
    char **value;
    int ptr;
    int size;
} OrderedStream;


OrderedStream *orderedStreamCreate(int n) {
    int size = n + 1;
    OrderedStream *obj = (OrderedStream *) malloc(sizeof(OrderedStream));
    obj->value = (char **) malloc(sizeof(char *) * (size));
    for (int i = 0; i < size; ++i) {
        obj->value[i] = 0;
    }
    obj->size = size;
    obj->ptr = 1;
    return obj;
}

char **orderedStreamInsert(OrderedStream *obj, int idKey, char *value, int *retSize) {
    obj->value[idKey] = value;
    char **res = (char **) malloc(sizeof(char *) * obj->size);
    int pos = 0;
    while (obj->ptr < obj->size && obj->value[obj->ptr]) {
        res[pos++] = obj->value[obj->ptr];
        obj->ptr++;
    }
    *retSize = pos;
    return res;
}

void orderedStreamFree(OrderedStream *obj) {
    free(obj->value);
    free(obj);
}

void out_print1656(char **res, int retsize) {
    for (int i = 0; i < retsize; ++i) {
        printf(res[i]);
        printf(" ");
    }
    printf("\n");
}

int main() {
    OrderedStream *obj = orderedStreamCreate(5);
    int retsize = 0;
    char **res = orderedStreamInsert(obj, 3, "ccccc", &retsize);
    out_print1656(res, retsize);

    res = orderedStreamInsert(obj, 1, "aaaaa", &retsize);
    out_print1656(res, retsize);

    res = orderedStreamInsert(obj, 2, "bbbbb", &retsize);
    out_print1656(res, retsize);

    res = orderedStreamInsert(obj, 5, "eeeee", &retsize);
    out_print1656(res, retsize);

    res = orderedStreamInsert(obj, 4, "ddddd", &retsize);
    out_print1656(res, retsize);

    orderedStreamFree(obj);
    return 0;
}

